package com.kkd.kkdapp.activity;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONException;
import org.json.JSONObject;

import pl.droidsonroids.gif.GifDrawable;

import com.google.gson.reflect.TypeToken;
import com.kkd.kkdapp.BaseApplication;
import com.kkd.kkdapp.Environment;
import com.kkd.kkdapp.R;
import com.kkd.kkdapp.Notify.Const;
import com.kkd.kkdapp.Notify.NotifyDispatcher;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.AllConfig;
import com.kkd.kkdapp.http.entity.HomeData;
import com.kkd.kkdapp.http.entity.HomePageData;
import com.kkd.kkdapp.http.entity.Register;
import com.kkd.kkdapp.http.entity.Verification;
import com.kkd.kkdapp.http.manager.HttpCallBack;

import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.listener.OnSingleClickListener;
import com.kkd.kkdapp.utils.DialogUtils;
import com.kkd.kkdapp.utils.LogUtil;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.utils.DiskCacheUtils;
import com.nostra13.universalimageloader.utils.MemoryCacheUtils;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources.NotFoundException;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.InputType;
import android.text.Selection;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

/**
 * 注册
 * 
 * @author mty
 * 
 */
public class RegisterActivity extends BaseActivity {

	private static final int ON_TIMER_STEP = 1;
	private static final int ON_GET_VERIFICATION_SUCCESS = 6;
	private static final int ON_GET_VERIFICATION_FAILURE = 7;
	private static final int ON_GET_YUYINCODE_SUCCESS = 8;
	private static final int ON_GET_YUYINCODE_FAILURE = 9;
	private static final int ON_REGISTER_SUCCESS = 10;
	private static final int ON_REGISTER_FAILURE = 12;
	private static final int GRAPHIC_CODE_SUCCESS = 2;
	private static final int GRAPHIC_CODE_FAILURE = 3;

	private Button register_button;// 注册按钮
	private Button get_tiyanjin_bt;// 注册按钮
	private TextView finish_bt;
	private RelativeLayout back;// 返回键
	private EditText phone_et;
	private EditText code_et;
	private EditText pwd_et;
	private EditText recommended_phone_et;
	private TextView sendphone_tv;
	private CheckBox is_show;
	private LinearLayout reg_centre;
	private LinearLayout reg_succeed_centre;
	private TextView tip_tv;
	private TextView coupon_count;
	private TextView reg_res_tv;
	private RelativeLayout get_code;
	private String channel_on; // 渠道号
	private String registerDesc;
	private Register mRegisterRes;
	private Dialog loadDialog;
	private String summary;
	private HomePageData mHomePageData;
	private HomeData mHomeData;
	private int count = 0;
	private TextView verification_time1;
	private TextView verification_time2;
	private Timer timer;
	private TimerTask mTimerTask;
	private TextView anew_tv;
	private TextView textView2;
	private TextView login_bt;
	private boolean isShowDailogInFortender = false;
	private ImageView graphic_code; // 图形验证码
	private RelativeLayout get_imgcode_rl; // 图形验证码栏
	private ImageView progressbr; // 图形验证码加载progress
	private RelativeLayout progressbr_rl; // 图形验证码加载progress边框
	private RelativeLayout refresh_bt; // 图形验证码刷新
	private String imageVCodeUrl; // 图形验证码Url
	private EditText get_imgcode_ed; // 图形验证码输入框

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case ON_REGISTER_SUCCESS:
				if (loadDialog != null) {
					loadDialog.dismiss();
				}
				String sinaUid = mRegisterRes.getData().getSinaUid();
				PreferenceUtils.getInstance().setSinaUid(RegisterActivity.this,
						sinaUid);
				PreferenceUtils.getInstance().setLoginSuccess(
						RegisterActivity.this, true);
				NotifyDispatcher.getInstance().sendNotify("",
						Const.NOTIFY_REGISTER_SUCCESS);
				Intent intent_toForTender = new Intent(RegisterActivity.this,
						ForTenderActivity.class);
				intent_toForTender.putExtra("isFromRegister",
						isShowDailogInFortender);
				intent_toForTender.putExtra("regSuccessImage", mRegisterRes
						.getData().getRegSuccessImage());
				startActivity(intent_toForTender);
				finish();
				break;
			case ON_REGISTER_FAILURE:
				showToast(registerDesc);
				if (loadDialog != null) {
					loadDialog.dismiss();
				}
				break;
			case ON_TIMER_STEP:
				if (count != 0) {
					verification_time1.setText("" + count);
					if (count < 40) {
						anew_tv.setVisibility(View.VISIBLE);
					}
					count--;
				} else if (count == 0) {
					verification_time1.setVisibility(View.INVISIBLE);
					// verification_time2.setVisibility(View.INVISIBLE);
					anew_tv.setVisibility(View.VISIBLE);
					if (mTimerTask != null) {
						mTimerTask.cancel();
						mTimerTask = null;
					}
				}
				break;
			case ON_GET_VERIFICATION_SUCCESS:
			case ON_GET_YUYINCODE_SUCCESS:
				showToast(summary);
				if (mYuyinCodeDialog != null) {
					mYuyinCodeDialog.cancel();
					mYuyinCodeDialog = null;
				}
				break;
			case ON_GET_VERIFICATION_FAILURE:
			case ON_GET_YUYINCODE_FAILURE:

				if (mYuyinCodeDialog != null) {
					mYuyinCodeDialog.cancel();
					mYuyinCodeDialog = null;
				}
				String sendMessage = msg.obj.toString();
				showToast(sendMessage);
				int statusCode = msg.arg1;
				if (statusCode == 268 || statusCode == 269 || statusCode == 270) {
					getGraphicCode(phone_et.getText().toString());
					if (timer != null) {
						timer.cancel();
						timer = null;
					}
					if (mTimerTask != null) {
						mTimerTask.cancel();
						mTimerTask = null;
					}
					count = 0;
					sendphone_tv.setVisibility(View.VISIBLE);
					anew_tv.setVisibility(View.GONE);
					verification_time1.setVisibility(View.GONE);
					if (!isMobileNO(phone_et.getText().toString())
							&& TextUtils.isEmpty(get_imgcode_ed.getText()
									.toString())) {
						sendphone_tv.setTextColor(0xffb5b5b5);
					} else {
						sendphone_tv.setTextColor(0xff6c54b6);
					}
				} else {
					count = 0;
					sendphone_tv.setVisibility(View.GONE);
					verification_time1.setVisibility(View.GONE);
					// verification_time2.setVisibility(View.GONE);
					anew_tv.setVisibility(View.VISIBLE);
				}
				break;

			case GRAPHIC_CODE_SUCCESS:
				try {
					String req = msg.obj.toString();
					JSONObject json = new JSONObject(req);
					imageVCodeUrl = json.getString("imageVCodeUrl");
					if (TextUtils.isEmpty(imageVCodeUrl)) {
						get_imgcode_rl.setVisibility(View.GONE);
						if (!isMobileNO(phone_et.getText().toString())) {
							sendphone_tv.setTextColor(0xffb5b5b5);
						} else {
							sendphone_tv.setTextColor(0xff6c54b6);
						}
					} else {
						get_imgcode_rl.setVisibility(View.VISIBLE);
						DiskCacheUtils.removeFromCache(imageVCodeUrl,
								ImageLoader.getInstance().getDiskCache());
						MemoryCacheUtils.removeFromCache(imageVCodeUrl,
								ImageLoader.getInstance().getMemoryCache());
						ImageLoader.getInstance().displayImage(imageVCodeUrl,
								graphic_code, new ImageLoadingListener() {

									@Override
									public void onLoadingStarted( // 开始
											String imageUri, View view) {
										progressbr_rl
												.setVisibility(View.VISIBLE);
									}

									@Override
									public void onLoadingFailed(
											// 失败
											String imageUri, View view,
											FailReason failReason) {
										progressbr_rl.setVisibility(View.GONE);

									}

									@Override
									public void onLoadingComplete(
											// 完成
											String imageUri, View view,
											Bitmap loadedImage) {
										progressbr_rl.setVisibility(View.GONE);
									}

									@Override
									public void onLoadingCancelled( // 取消
											String imageUri, View view) {

									}
								});
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				break;
			case GRAPHIC_CODE_FAILURE:
				imageVCodeUrl = "";
				String desc = msg.obj.toString();
				showToast(desc);
				if (!isMobileNO(phone_et.getText().toString())) {
					sendphone_tv.setTextColor(0xffb5b5b5);
				} else {
					sendphone_tv.setTextColor(0xff6c54b6);
				}
				break;
			default:
				break;
			}
		};
	};
	private View rel_new_task;
	private TextView tv_tip_black1;
	private TextView tv_tip_orange;
	private TextView tv_tip_black2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		mHomeData = PreferenceUtils.getInstance().getHomeData2(this);
		mHomePageData = PreferenceUtils.getInstance().getHomePageData(this);
		initView();
		setListener();
		getMetaData();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (loadDialog != null) {
			loadDialog.cancel();
			loadDialog = null;
		}
		if (timer != null) {
			timer.cancel();
			timer = null;
		}
		if (mTimerTask != null) {
			mTimerTask.cancel();
			mTimerTask = null;
		}
	}

	public void initView() {
		/*
		 * 以下为新手任务相关
		 */
		rel_new_task = findViewById(R.id.rel_new_task);
		tv_tip_black1 = (TextView) findViewById(R.id.tv_tip_black1);
		tv_tip_orange = (TextView) findViewById(R.id.tv_tip_orange);
		tv_tip_black2 = (TextView) findViewById(R.id.tv_tip_black2);
		AllConfig allConfigData = PreferenceUtils.getInstance()
				.getAllConfigData(this);
		if (allConfigData != null) {
			if (allConfigData.getRegtips() != null) {
				if (!TextUtils.isEmpty(allConfigData.getRegtips()
						.getRegBefTips())) {
					rel_new_task.setVisibility(View.VISIBLE);
					isShowDailogInFortender = true;
				} else {
					rel_new_task.setVisibility(View.GONE);
					isShowDailogInFortender = false;
				}
			} else {
				rel_new_task.setVisibility(View.GONE);
				isShowDailogInFortender = false;
			}
		} else {
			rel_new_task.setVisibility(View.GONE);
			isShowDailogInFortender = false;
		}
		/*
		 * 以上为新手任务相关
		 */
		reg_centre = (LinearLayout) findViewById(R.id.reg_centre);
		get_tiyanjin_bt = (Button) findViewById(R.id.get_tiyanjin_bt);
		finish_bt = (TextView) findViewById(R.id.finish_bt);
		reg_succeed_centre = (LinearLayout) findViewById(R.id.reg_succeed_centre);
		reg_succeed_centre.setVisibility(View.GONE);
		tip_tv = (TextView) findViewById(R.id.tip_tv);
		coupon_count = (TextView) findViewById(R.id.coupon_count);
		reg_res_tv = (TextView) findViewById(R.id.reg_res_tv);
		reg_centre.setVisibility(View.VISIBLE);
		register_button = (Button) findViewById(R.id.register_button);

		if (mHomeData != null) {
			if (!TextUtils.isEmpty(mHomeData.getOther().getRgBeforePrompt()))
				register_button.setText(mHomeData.getOther()
						.getRgBeforePrompt());
			// if(mHomePageData.getIsHaveBinCard()==0)
			if (!TextUtils.isEmpty(mHomeData.getOther().getRgAfterPrompt()))
				get_tiyanjin_bt
						.setText(mHomeData.getOther().getRgAfterPrompt());
			// if(mHomePageData.getIsHaveBinCard()==0)
			if (!TextUtils.isEmpty(mHomeData.getOther().getRgAfterPrompt()))
				get_tiyanjin_bt
						.setText(mHomeData.getOther().getRgAfterPrompt());
		}
		back = (RelativeLayout) findViewById(R.id.back);// 返回键
		phone_et = (EditText) findViewById(R.id.phone_et);
		code_et = (EditText) findViewById(R.id.code_et);
		pwd_et = (EditText) findViewById(R.id.pwd_et);
		recommended_phone_et = (EditText) findViewById(R.id.recommended_phone_et);
		is_show = (CheckBox) findViewById(R.id.is_show);
		get_code = (RelativeLayout) findViewById(R.id.get_code);
		sendphone_tv = (TextView) findViewById(R.id.sendphone_tv);
		verification_time1 = (TextView) this
				.findViewById(R.id.verification_time1);
		verification_time2 = (TextView) this
				.findViewById(R.id.verification_time2);
		anew_tv = (TextView) this.findViewById(R.id.anew_tv);
		textView2 = (TextView) this.findViewById(R.id.textView2);
		login_bt = (TextView) this.findViewById(R.id.login_bt);
		recommended_phone_et
				.setOnEditorActionListener(new OnEditorActionListener() {

					@Override
					public boolean onEditorAction(TextView v, int actionId,
							KeyEvent event) {
						if (actionId == EditorInfo.IME_ACTION_DONE) {
							registerControl();
						}
						return false;
					}
				});
		pwd_et.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_NEXT) {
					ActionSendVerification();
				}
				return false;
			}
		});
		graphic_code = (ImageView) this.findViewById(R.id.graphic_code);
		get_imgcode_rl = (RelativeLayout) this
				.findViewById(R.id.get_imgcode_rl);
		progressbr = (ImageView) this.findViewById(R.id.progressbr);
		GifDrawable drawable;
		try {
			drawable = new GifDrawable(RegisterActivity.this.getResources(),
					R.drawable.loading_gif);
			progressbr.setImageDrawable(drawable);
		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		progressbr_rl = (RelativeLayout) this.findViewById(R.id.progressbr_rl);
		refresh_bt = (RelativeLayout) this.findViewById(R.id.refresh_bt);
		refresh_bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ImageLoader imageLoder = ImageLoader.getInstance();
				DiskCacheUtils.removeFromCache(imageVCodeUrl,
						imageLoder.getDiskCache());
				MemoryCacheUtils.removeFromCache(imageVCodeUrl,
						imageLoder.getMemoryCache());
				ImageLoader.getInstance().displayImage(imageVCodeUrl,
						graphic_code, new ImageLoadingListener() {

							@Override
							public void onLoadingStarted( // 开始
									String imageUri, View view) {
								LogUtil.e("TAG", "加载开始");
								progressbr_rl.setVisibility(View.VISIBLE);
							}

							@Override
							public void onLoadingFailed(
									// 失败
									String imageUri, View view,
									FailReason failReason) {
								progressbr_rl.setVisibility(View.GONE);

							}

							@Override
							public void onLoadingComplete(
									// 完成
									String imageUri, View view,
									Bitmap loadedImage) {
								progressbr_rl.setVisibility(View.GONE);
							}

							@Override
							public void onLoadingCancelled( // 取消
									String imageUri, View view) {

							}
						});
			}
		});
		get_imgcode_ed = (EditText) this.findViewById(R.id.get_imgcode_ed);
	}

	public void setListener() {
		register_button.setOnClickListener(mOnSingleClickListener);
		back.setOnClickListener(mOnSingleClickListener);
		is_show.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					// 显示为普通文本
					pwd_et.setInputType(145);
					// 使光标始终在最后位置
					Editable etable = pwd_et.getText();
					Selection.setSelection(etable, etable.length());
				} else {
					// 显示为密码
					pwd_et.setInputType(129);
					// 使光标始终在最后位置
					Editable etable = pwd_et.getText();
					Selection.setSelection(etable, etable.length());
				}
			}
		});
		sendphone_tv.setOnClickListener(mOnSingleClickListener);
		get_tiyanjin_bt.setOnClickListener(mOnSingleClickListener);
		finish_bt.setOnClickListener(mOnSingleClickListener);
		anew_tv.setOnClickListener(mOnSingleClickListener);
		textView2.setOnClickListener(mOnSingleClickListener);
		login_bt.setOnClickListener(mOnSingleClickListener);
		phone_et.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if (!TextUtils.isEmpty(imageVCodeUrl)
						&& isMobileNO(phone_et.getText().toString())
						&& !TextUtils.isEmpty(get_imgcode_ed.getText()
								.toString())) {
					sendphone_tv.setTextColor(0xff6c54b6);
				}
				if (phone_et.getText().toString().length() == 11
						&& isMobileNO(phone_et.getText().toString())) {
					getGraphicCode(phone_et.getText().toString());
				} else {
					sendphone_tv.setTextColor(0xffb5b5b5);
					get_imgcode_rl.setVisibility(View.GONE);
				}
			}
		});
		get_imgcode_ed.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				if (isMobileNO(phone_et.getText().toString())
						&& !TextUtils.isEmpty(get_imgcode_ed.getText()
								.toString())) {
					sendphone_tv.setTextColor(0xff6c54b6);
				} else {
					sendphone_tv.setTextColor(0xffb5b5b5);
				}
			}
		});
	}

	/**
	 * 获取渠道号
	 */
	public void getMetaData() {
		try {
			ApplicationInfo appInfo = this.getPackageManager()
					.getApplicationInfo(getPackageName(),
							PackageManager.GET_META_DATA);
			channel_on = appInfo.metaData.getString("UMENG_CHANNEL");
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
	}

	OnSingleClickListener mOnSingleClickListener = new OnSingleClickListener() {

		@Override
		public void onSingleClick(View v) {
			switch (v.getId()) {
			case R.id.register_button:// 注册按钮
				registerControl();
				break;
			case R.id.back:// 返回键
				setResult(100);
				finish();
				break;
			case R.id.sendphone_tv:
				ActionSendVerification();

				break;
			case R.id.get_tiyanjin_bt:
				SystemUtils.onEvent("Register_Submit6888", 1);
				if (get_tiyanjin_bt.getText().toString().equals("确认")) {
					finish();
				} else {
					Intent intent = new Intent(RegisterActivity.this,
							ForTenderActivity.class);
					intent.putExtra("isShowDialogTip", true);
					startActivity(intent);
					finish();
				}

				break;
			case R.id.finish_bt:
				SystemUtils.onEvent("Register_Back", 1);
				finish();
				break;
			case R.id.anew_tv:
				SystemUtils.onEvent("Register_NOPhoneCode", 1);
				if (count == 0) {
					showYuyinCodeDialog();
				} else {
					sendYuyinRequest();
					SystemUtils.onEvent("Register_TelCode", 1);
				}
				break;
			case R.id.textView2: // 协议
				SystemUtils.onEvent("Register_Agreement", 1);
				Intent intent = new Intent(RegisterActivity.this,
						KKDWebActivity.class);
				intent.putExtra("url", Environment.getH5Url()
						+ "baseservice.html");
				intent.putExtra("titleName", "快快贷理财用户协议");
				startActivity(intent);
				break;
			case R.id.login_bt: // 登录
				Intent intent1 = new Intent(RegisterActivity.this,
						LoginActivity.class);
				intent1.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent1);
				finish();
				break;
			}
		}
	};

	public void countDown() {
		sendphone_tv.setVisibility(View.GONE);
		verification_time1.setVisibility(View.VISIBLE);
		verification_time1.setText("" + count);
		// verification_time2.setVisibility(View.VISIBLE);
		anew_tv.setVisibility(View.GONE);
		if (timer == null)
			timer = new Timer();
		if (mTimerTask == null)
			mTimerTask = new TimerTask() {

				@Override
				public void run() {
					handler.sendEmptyMessage(ON_TIMER_STEP);
				}
			};
		timer.schedule(mTimerTask, 00, 1000);
	}

	protected void ActionSendVerification() {
		String phone = phone_et.getText().toString();
		if (TextUtils.isEmpty(phone)) {
			showToast("请输入正确的手机号");
			return;
		} else if (!isMobileNO(phone)) {
			showToast(R.string.phone_on);
			return;
		} else {
			if (!TextUtils.isEmpty(imageVCodeUrl)) { // 需要图形验证码
				if (TextUtils.isEmpty(get_imgcode_ed.getText().toString())) {
					showToast("请输入正确的图形验证码");
				} else {
					if (count == 0) {
						sendVerification();
						count = 60;
						countDown();
					}
				}
			} else { // 不需要图形验证码
				if (count == 0) {
					sendVerification();
					count = 60;
					countDown();
				}
			}
		}
	}

	/**
	 * showPopuWindow:(这里用一句话描述这个方法的作用). <br/>
	 * 
	 * @author askzhao
	 * @param v
	 */
	private void showPopuWindow(View v) {
		LayoutInflater inflater = LayoutInflater.from(this);
		View view = inflater.inflate(R.layout.register_tip_window, null);
		final PopupWindow pop = new PopupWindow(view,
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, false);
		pop.setOutsideTouchable(true);
		RelativeLayout close_tv = (RelativeLayout) view
				.findViewById(R.id.close_tv);
		pop.showAtLocation(v, 0, 0, 0);
		close_tv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				pop.dismiss();
				final WindowManager.LayoutParams attrs = getWindow()
						.getAttributes();
				attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
				getWindow().setAttributes(attrs);
				getWindow().clearFlags(
						WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
			}
		});

	}

	/**
	 * 注册按钮控制
	 */
	public void registerControl() {
		String phone = phone_et.getText().toString();
		if (TextUtils.isEmpty(phone)) {
			showToast("请输入正确的手机号");
			return;
		}

		if (!isMobileNO(phone)) {
			showToast(R.string.phone_on);
			return;
		}
		String code = code_et.getText().toString();
		if (TextUtils.isEmpty(code)) {
			showToast("请输入正确的验证码");
			return;
		}

		String psw = pwd_et.getText().toString();
		if (TextUtils.isEmpty(psw)) {
			showToast("请输入登录密码");
			return;
		}

		if (psw.length() < 6) {
			showToast("密码长度不能小于6位");
			return;
		}

		if (!"".equals(recommended_phone_et.getText().toString())) {
			if (isMobileNO(recommended_phone_et.getText().toString())) {
				if (phone.equals(recommended_phone_et.getText().toString())) {
					showToast(R.string.recommend_phone);
				} else {
					register();
				}
			} else {
				showToast(R.string.phone_mistake);
			}
		} else {
			register();
		}

	}

	/**
	 * 注册成功接口
	 */
	public void register() {
		if (loadDialog != null) {
			loadDialog.cancel();
			loadDialog = null;
		}
		loadDialog = DialogUtils.toLoadDialog(this, "努力加载中....");
		KkdHttpMagager.getInstance().getRegisterManager(
				phone_et.getText().toString(), pwd_et.getText().toString(),
				pwd_et.getText().toString(),
				recommended_phone_et.getText().toString(), channel_on,
				code_et.getText().toString(), Register.class,
				new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object res) {
						mRegisterRes = (Register) res;
						handler.sendEmptyMessage(ON_REGISTER_SUCCESS);
					}

					@Override
					public void onFailure(int statusCode, String responseBody) {
						registerDesc = responseBody;
						handler.sendEmptyMessage(ON_REGISTER_FAILURE);
					}
				});

	}

	private Dialog mYuyinCodeDialog;

	protected void showYuyinCodeDialog() {
		if (mYuyinCodeDialog != null) {
			mYuyinCodeDialog.cancel();
			mYuyinCodeDialog = null;
		}

		mYuyinCodeDialog = DialogUtils.YuyinCodeDialog(this, phone_et.getText()
				.toString(), new OnClickListener() {

			@Override
			public void onClick(View v) {
				sendYuyinRequest();
				SystemUtils.onEvent("Register_TelCode", 1);
			}
		}, new OnClickListener() {

			@Override
			public void onClick(View v) {
				SystemUtils.onEvent("Register_ReMessageCode", 1);
				String phone = phone_et.getText().toString();
				if (TextUtils.isEmpty(phone)) {
					showToast("请输入正确的手机号");
					return;
				} else if (!isMobileNO(phone)) {
					showToast(R.string.phone_on);
					return;
				} else {

					if (!TextUtils.isEmpty(imageVCodeUrl)) { // 需要图形验证码
						if (TextUtils.isEmpty(get_imgcode_ed.getText()
								.toString())) {
							showToast("请输入正确的图形验证码");
						} else {
							if (count == 0) {
								sendVerification();
								count = 60;
								countDown();
							}
						}
					} else { // 不需要图形验证码
						if (count == 0) {
							sendVerification();
							count = 60;
							countDown();
						}
					}

				}

			}
		}, new OnClickListener() {

			@Override
			public void onClick(View v) {
				mYuyinCodeDialog.dismiss();
			}
		});
		mYuyinCodeDialog.show();

	}

	/**
	 * 发送语音验证码
	 */
	public void sendYuyinRequest() {
		KkdHttpMagager.getInstance().getYuyinRequest(
				get_imgcode_ed.getText().toString(),
				phone_et.getText().toString(), Verification.class,
				new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						Verification verification = (Verification) responseBody;
						summary = verification.getSummary();
						handler.sendEmptyMessage(ON_GET_YUYINCODE_SUCCESS);

					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = new Message();
						msg.obj = desc;
						msg.arg1 = statusCode;
						msg.what = ON_GET_YUYINCODE_FAILURE;
						handler.sendMessage(msg);
					}
				});
	}

	/**
	 * 
	 * 发送验证码
	 */
	public void sendVerification() {

		KkdHttpMagager.getInstance().getPhoneRegisterManager(
				get_imgcode_ed.getText().toString(),
				phone_et.getText().toString(), new TypeToken<Verification>() {
				}.getType(), new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						Verification verification = (Verification) responseBody;
						summary = verification.getSummary();
						handler.sendEmptyMessage(ON_GET_VERIFICATION_SUCCESS);

					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = new Message();
						msg.obj = desc;
						msg.arg1 = statusCode;
						msg.what = ON_GET_VERIFICATION_FAILURE;
						handler.sendMessage(msg);
					}
				});

	}

	/**
	 * 图形验证码
	 */
	public void getGraphicCode(String phone) {
		KkdHttpMagager.getInstance().getGraphicCode(phone, String.class,
				new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						Message msg = new Message();
						msg.what = GRAPHIC_CODE_SUCCESS;
						msg.obj = responseBody;
						handler.sendMessage(msg);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = new Message();
						msg.what = GRAPHIC_CODE_FAILURE;
						msg.obj = desc;
						handler.sendMessage(msg);
					}
				});
	}
}
